package com.eastcom.providor.service.impl;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;

import com.eastcom.bean.DataSetCollectBean;
import com.eastcom.bm.atom.Atom_DatasetDefine;
import com.eastcom.bm.atom.Atom_DatasetNe;
import com.eastcom.bm.atom.Atom_KpiDefine;
import com.eastcom.bm.atom.Atom_NeSpecialThreadhold;
import com.eastcom.bm.atom.CollectMonitor;
import com.eastcom.bm.atom.DataSetStatiBean;
import com.eastcom.bm.atom.ReportMapping;
import com.eastcom.bm.dataprocess.processor.CacheEditBean;

import com.eastcom.canicula.stope.utils.DateUtil;
import com.eastcom.client.CollectDescBean;
import com.eastcom.client.DataAbnormalStateBean;
import com.eastcom.client.DataSetStateBean;
import com.eastcom.client.DatasetBean;
import com.eastcom.client.IntegerDescBean;
import com.eastcom.client.KpiAndThBean;
import com.eastcom.client.KpiStateBean;
import com.eastcom.client.MapEntryNode;
import com.eastcom.client.NeTypeDataSetBean;
import com.eastcom.client.NeTypeDataSetStatiBean;
import com.eastcom.client.ReturnNode;
import com.eastcom.client.TreeNode;
import com.eastcom.common.util.CharliDate;
import com.eastcom.dataquality.cc.CollectService;
import com.eastcom.dataquality.cc.CollectType;
import com.eastcom.notice.NoticeSender;
import com.eastcom.providor.BMSetting;
import com.eastcom.providor.dao.IDataAbnormalDao;
import com.eastcom.providor.dao.IDataSetDao;
import com.eastcom.providor.dao.IKpiDefineDao;
import com.eastcom.providor.dao.IRawDataDao;
import com.eastcom.providor.dao.impl.CollectMonitorDao;
import com.eastcom.providor.dao.impl.DataSetDao;
import com.eastcom.providor.dao.impl.ReportMappingDao;
import com.eastcom.providor.service.BaseService;
import com.eastcom.providor.service.IDataSetService;
import com.eastcom.providor.service.extview.CollectMonitorUtil;
import com.eastcom.providor.service.extview.DatasetDefineUtil;
import com.eastcom.providor.service.extview.KpiAndThreadholdUtil;

public class DataSetService extends BaseService implements IDataSetService{

	private IDataSetDao dataSetDao;
	
	private IDataAbnormalDao dataAbnormalDao;
	
	private IKpiDefineDao kpiDefineDao;
	
	private IRawDataDao rawDataDao;
	//private IDataSetNeDao dataSetNeDao;
	private ReportMappingDao mappingDao;

	private CollectService service;

	private String rmiUrl;
	
	private CollectMonitorDao monitorDao;
	

	static SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm");
	
	//public void setDataSetNeDao(IDataSetNeDao dataSetNeDao) {
		//this.dataSetNeDao = dataSetNeDao;
	//}
    public void Test(String a){
    	System.out.println(a);
    }

	public void setKpiDefineDao(IKpiDefineDao kpiDefineDao) {
		this.kpiDefineDao = kpiDefineDao;
	}

	public void setDataSetDao(IDataSetDao dataSetDao) {
		this.dataSetDao = dataSetDao;
	}
	
	public void setMappingDao(ReportMappingDao mappingDao) {
		this.mappingDao = mappingDao;
	}

	public void setMonitorDao(CollectMonitorDao monitorDao) {
		this.monitorDao = monitorDao;
	}
	
	public void setDataAbnormalDao(IDataAbnormalDao dataAbnormalDao) {
		this.dataAbnormalDao = dataAbnormalDao;
	}
	
	public void setRawDataDao(IRawDataDao rawDataDao) {
		this.rawDataDao = rawDataDao;
	}
	
	public String getRmiUrl() {
		return rmiUrl;
	}

	public void setRmiUrl(String rmiUrl) {
		this.rmiUrl = rmiUrl;
	}
	
	public void init(){
		System.setProperty("sun.rmi.dgc.ackTimeout", "10000");
		System.setProperty("sun.rmi.transport.tcp.readTimeout", "10000");
		System.setProperty("sun.rmi.transport.connectionTimeout", "10000");
		RmiProxyFactoryBean rmiFactoryBean = new RmiProxyFactoryBean();
		rmiFactoryBean.setServiceUrl(rmiUrl);
		rmiFactoryBean.setServiceInterface(CollectService.class);
		rmiFactoryBean.setLookupStubOnStartup(false);
		rmiFactoryBean.setRefreshStubOnConnectFailure(true);
		rmiFactoryBean.afterPropertiesSet();
		service = (CollectService) rmiFactoryBean.getObject();
	}

	/**
	 * 下发到采集端，并通知校验端采用新的校验规则
	 * */
	public boolean sendDataSetCollectBean(String dataSetId){
		boolean ret=false;
		try {
			ret=(Boolean)service.process("normal", "add", dataSetId);
		} catch (Exception e) {
			e.printStackTrace();
			ret=false;
		}
		return ret;
	}

	/**
	 * 此接口已不能使用
	 * 重新下发
	 * */
	@Deprecated
	public boolean sendReCollectDataSetCollectBean(String dataSetId){
		Atom_DatasetDefine dataSet;
		boolean success=false;
		try {
			dataSet = dataSetDao.findById(dataSetId, true);
			if(dataSet==null)
				return success;
			
			DataSetCollectBean collectBean=this.wrapReCollectDataSetCollectBeanByDataSet(dataSet);
//			success = noticeAll(dataSetId,collectBean);
		} 
		catch (Exception e) {
			log.error("DataSetService sendReCollectDataSetCollectBean error", e);
			e.printStackTrace();
		}
		return success;
	}
	
	private boolean noticeAllByModifyDataSet(String dataSetId,Atom_DatasetDefine dataSet) {
		NoticeSender sender=NoticeSender.getInstance();
		Lock lock=sender.getNewLock();
		lock.lock();
		boolean ret=true;
		try{
			//在此增加是否是第一次下发的判断
			DataSetCollectBean collectBean=null;
			if("1".equals(dataSet.getFirstCollect())){
				collectBean=this.wrapDataSetCollectBeanByDataSet(dataSet);
				dataSet.setFirstCollect("0");
				dataSetDao.updateDatasetDefine(dataSet);
			}else{
				collectBean=this.wrapReCollectDataSetCollectBeanByDataSet(dataSet);
			}
		    sender.sendObject(BMSetting.host, BMSetting.port, collectBean);
		    log.info("DataSetService sendDataSetCollectBean with dataSetId: "+dataSetId+" completed!");
		}
		catch(Exception e){
			log.error("DataSetService sendDataSetCollectBean with dataSetId: "+dataSetId+" error!",e);
			e.printStackTrace();
			ret=false;
		}
		
		try{
			CacheEditBean cacheEditBean=new CacheEditBean();
			cacheEditBean.setBeanClass(Atom_DatasetDefine.class);
			cacheEditBean.setBeanId(dataSetId);
			cacheEditBean.setOperationType("edit");
			sender.sendObject(BMSetting.dataCollectorHost, BMSetting.dataCollectorPort, cacheEditBean, false);
			log.info("DataSetService send dataSetId "+dataSetId+" to dataCollector completed");
		}
		catch(Exception e){
			log.error("DataSetService send dataSetId "+dataSetId+" to dataCollector error", e);
			ret=false;
		}
		
		lock.unlock();
		return ret;
	}
	
	public DataSetCollectBean wrapDataSetCollectBeanByDataSet(Atom_DatasetDefine dataSet){
		DataSetCollectBean collectBean=new DataSetCollectBean();
		collectBean.setDbType(dataSet.getDbType());
		collectBean.setHost(dataSet.getHost());
		collectBean.setPort(dataSet.getPort());
		collectBean.setSid(dataSet.getSid());
		collectBean.setUserName(dataSet.getUserName());
		collectBean.setPassWord(dataSet.getPassWord());
		
		collectBean.setCollectSql(dataSetDao.generateCollectSql(dataSet));
		collectBean.setFirstStartTime(dataSet.getFirstStartTime());
		collectBean.setFirstEndTime(dataSet.getFirstEndTime());
		collectBean.setStartCollectTime(dataSet.getStartCollectTime());
		collectBean.setTimerCrontab(dataSet.getTimerCrontab());
		collectBean.setGranularity(dataSet.getGranularity());
		collectBean.setTimeUnit(dataSet.getTimeUnit());
		collectBean.setDataSetId(dataSet.getId());
		collectBean.setBackCollectNum(dataSet.getBackCollectNum());
		
		return collectBean;
	}
	
	/**
	 * 界面点击"重新下发"时将采用这种形式的Bean
	 * */
	public DataSetCollectBean wrapReCollectDataSetCollectBeanByDataSet(Atom_DatasetDefine dataSet){
		DataSetCollectBean collectBean=new DataSetCollectBean();
		collectBean.setDbType(dataSet.getDbType());
		collectBean.setHost(dataSet.getHost());
		collectBean.setPort(dataSet.getPort());
		collectBean.setSid(dataSet.getSid());
		collectBean.setUserName(dataSet.getUserName());
		collectBean.setPassWord(dataSet.getPassWord());
		
		collectBean.setCollectSql(dataSetDao.generateCollectSql(dataSet));
		collectBean.setFirstStartTime(null);
		collectBean.setFirstEndTime(null);
		collectBean.setStartCollectTime(dataSet.getStartCollectTime());
		collectBean.setTimerCrontab(dataSet.getTimerCrontab());
		collectBean.setGranularity(dataSet.getGranularity());
		collectBean.setTimeUnit(dataSet.getTimeUnit());
		collectBean.setDataSetId(dataSet.getId());
		collectBean.setBackCollectNum(dataSet.getBackCollectNum());
		
		return collectBean;
	}
	
	public List<DataSetCollectBean> selectAllDataSetCollectBean(){
		try{
			List<Atom_DatasetDefine> dataSets=dataSetDao.selectDatasetDefineByNeTypeAndCName(null, null, true);
			if(dataSets!=null&&!dataSets.isEmpty()){
				List<DataSetCollectBean> results=new ArrayList<DataSetCollectBean>();
				for(Atom_DatasetDefine dataSet:dataSets)
					results.add(this.wrapDataSetCollectBeanByDataSet(dataSet));
				
				return results;
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	//WB-批量告警确认
	public List<DataAbnormalStateBean> getIntegerDataAbnormalStateBeansByParamWB(String dataSetId,List dataTimes) throws ParseException{
		List<DataAbnormalStateBean> listdas=new ArrayList<DataAbnormalStateBean>();
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(dataTimes!=null){
			List<Date> timeList=new ArrayList();
			for(int i=0;i<dataTimes.size();i++){
				//Date date=formatter.parse(dataTimes.get(i).toString());
				Date date=new Date(Long.parseLong(dataTimes.get(i).toString()));
				timeList.add(date);
			}
			listdas=this.getIntegerDataAbnormalStateBeansByParam(dataSetId, timeList);
		}else if(dataTimes==null){
			return null;
		}
		return listdas;
	}
	
	public List<DataAbnormalStateBean> getIntegerDataAbnormalStateBeansByParam(String dataSetId,List<Date> dataTimes){
		if(dataTimes==null||dataTimes.isEmpty())
			return null;
		
		try{
			List<DataAbnormalStateBean> dataAbnormalStateBeans=new ArrayList<DataAbnormalStateBean>();
			List<Date> integerTimeStamp=new ArrayList<Date>();
			Atom_DatasetDefine dataSet=dataSetDao.findById(dataSetId, false);
			
			Map<Object,Map> dimensionNumMap=dataSetDao.selectDimensionNumByParamsFromSource(dataSetId, dataTimes);
			for(Date date:dataTimes){
				DataAbnormalStateBean dataAbnormalStateBean=new DataAbnormalStateBean();
				dataAbnormalStateBean.setAbnormalType("4");
				dataAbnormalStateBean.setTitle("完整性异常检查");
				dataAbnormalStateBean.setTimeColumn(dataSet.getTimeStampColumn());
				dataAbnormalStateBean.setTimeStamp(date);
				
				Map map=dimensionNumMap.get(date);
				Integer integerNum=map==null||map.get("num")==null?0:((Number)map.get("num")).intValue();
				boolean isNormal=integerNum>=dataSet.getBaseIntegerValue()*(1-dataSet.getDeviation()/100)&&integerNum<=dataSet.getBaseIntegerValue()*(1+dataSet.getDeviation()/100);
				if(isNormal)
					integerTimeStamp.add(date);
				
				dataAbnormalStateBean.setNormal(isNormal);
				dataAbnormalStateBean.setBaseIntegerValue(dataSet.getBaseIntegerValue());
				dataAbnormalStateBean.setDeviation(dataSet.getDeviation());
				String desc="完整性异常 当前时间点维度个数 "+integerNum+",完整性基准值 "+dataSet.getBaseIntegerValue()+",偏离度 "+dataSet.getDeviation()+"%";
				dataAbnormalStateBean.setDesc(desc);
				
				dataAbnormalStateBeans.add(dataAbnormalStateBean);
			}
			
			dataAbnormalDao.removeIntegerAbnormals(dataSetId, integerTimeStamp);
			log.info("DataSetService getIntegerDataAbnormalStateBeansByParam remove dataSet "+dataSetId+" with timeStamp size "+integerTimeStamp.size());
			return dataAbnormalStateBeans;
		}
		catch(Exception e){
			//log.error("DataSetService getIntegerDataAbnormalStateBeansByParam error", e);
			e.printStackTrace();
		}
		return null;
	}
	
	public DataAbnormalStateBean getDataAbnormalStateBeanByParam(String dataSetId, String kpiId, String dimension, String abnormalType, String validationType, Date dataTime,String avgHistoryValue) {
		try{
			DataAbnormalStateBean dataAbnormalStateBean=new DataAbnormalStateBean();
			dataAbnormalStateBean.setAbnormalType(abnormalType);
			String title="4".equals(abnormalType)?"完整性异常检查":"0".equals(abnormalType)?"门限值异常检查":"1".equals(abnormalType)?"基准值异常检查":"历史值异常检查";
			dataAbnormalStateBean.setTitle(title);
			dataAbnormalStateBean.setNeId(dimension);
			Atom_DatasetDefine dataSet=dataSetDao.findById(dataSetId, false);
			dataAbnormalStateBean.setTimeColumn(dataSet.getTimeStampColumn());
			
			if("4".equals(abnormalType)){
				int integerNum=dataSetDao.selectDimensionNumByParamsFromSource(dataSetId, dataTime);
				
				boolean isNormal=integerNum>=dataSet.getBaseIntegerValue()*(1-dataSet.getDeviation()/100)&&integerNum<=dataSet.getBaseIntegerValue()*(1+dataSet.getDeviation()/100);
				dataAbnormalStateBean.setNormal(isNormal);
				dataAbnormalStateBean.setBaseIntegerValue(dataSet.getBaseIntegerValue());
				dataAbnormalStateBean.setDeviation(dataSet.getDeviation());
				String desc="完整性异常 当前时间点维度个数 "+integerNum+",完整性基准值 "+dataSet.getBaseIntegerValue()+",偏离度 "+dataSet.getDeviation()+"%";
				dataAbnormalStateBean.setDesc(desc);
				if(isNormal)
					dataAbnormalDao.removeIntegerAbnormal(dataSetId, dataTime);
			}
			else if("0".equals(abnormalType)){
				List<Map> values=dataSetDao.selectHistoryDataValueByParamsFromSource(dataSetId, kpiId, dimension, dataTime);
				Atom_KpiDefine kpiDefine=kpiDefineDao.findKpiDefineByDataSetIdAndKpiId(dataSetId, kpiId);
				dataAbnormalStateBean.setThreadholdDown(kpiDefine.getThreadHoldDown());
				dataAbnormalStateBean.setThreadholdUp(kpiDefine.getThreadHoldUp());
				dataAbnormalStateBean.setKpiColumn(kpiDefine.getColumnName());
				
				String value=values==null||values.isEmpty()||values.get(values.size()-1).get(kpiDefine.getColumnName())==null?null:
					values.get(values.size()-1).get(kpiDefine.getColumnName()).toString();
				boolean isNormal=value==null?false:Double.parseDouble(value)>=kpiDefine.getThreadHoldDown()&&Double.parseDouble(value)<=kpiDefine.getThreadHoldUp();
				dataAbnormalStateBean.setNormal(isNormal);
				String desc="准确性异常 当前时间点指标值 "+value+",上门限 "+kpiDefine.getThreadHoldUp()+",下门限 "+kpiDefine.getThreadHoldDown()+"%";
				dataAbnormalStateBean.setDesc(desc);
				dataAbnormalStateBean.setHistoryValues(values);
				if(isNormal)
					dataAbnormalDao.removeDataAbnormal(dataSetId, kpiId, dimension, dataTime);
				
			}
			else if("1".equals(abnormalType)){
				List<Map> values=dataSetDao.selectHistoryDataValueByParamsFromSource(dataSetId, kpiId, dimension, dataTime);
				Atom_KpiDefine kpiDefine=kpiDefineDao.findKpiDefineByDataSetIdAndKpiId(dataSetId, kpiId);
				dataAbnormalStateBean.setThreadholdDown(kpiDefine.getThreadHoldDown());
				dataAbnormalStateBean.setThreadholdUp(kpiDefine.getThreadHoldUp());
				dataAbnormalStateBean.setKpiColumn(kpiDefine.getColumnName());
				
				String value=values==null||values.isEmpty()||values.get(values.size()-1).get(kpiDefine.getColumnName())==null?null:
					values.get(values.size()-1).get(kpiDefine.getColumnName()).toString();
				boolean isNormal=value==null?false:Double.parseDouble(value)>=kpiDefine.getBaselineValue()*(1-kpiDefine.getDeviationDown()/100)
						&&Double.parseDouble(value)<=kpiDefine.getBaselineValue()*(1+kpiDefine.getDeviationUp()/100);
				dataAbnormalStateBean.setNormal(isNormal);
				String desc="准确性异常 当前时间点指标值 "+value+",上门限 "+kpiDefine.getThreadHoldUp()+",下门限 "+kpiDefine.getThreadHoldDown()+"%";
				dataAbnormalStateBean.setDesc(desc);
				dataAbnormalStateBean.setHistoryValues(values);
				if(isNormal)
					dataAbnormalDao.removeDataAbnormal(dataSetId, kpiId, dimension, dataTime);
			}
			else{
				List<Map> values=dataSetDao.selectHistoryDataValueByParamsFromSource(dataSetId, kpiId, dimension, dataTime);
				Atom_KpiDefine kpiDefine=kpiDefineDao.findKpiDefineByDataSetIdAndKpiId(dataSetId, kpiId);
				dataAbnormalStateBean.setThreadholdDown(kpiDefine.getThreadHoldDown());
				dataAbnormalStateBean.setThreadholdUp(kpiDefine.getThreadHoldUp());
				dataAbnormalStateBean.setKpiColumn(kpiDefine.getColumnName());
				
				String value=values==null||values.isEmpty()||values.get(values.size()-1).get(kpiDefine.getColumnName())==null?null:
					values.get(values.size()-1).get(kpiDefine.getColumnName()).toString();
				
				boolean isNormal=value==null||avgHistoryValue==null?false:Double.parseDouble(value)>=Double.parseDouble(avgHistoryValue)*(1-kpiDefine.getDeviationDown()/100)
						&&Double.parseDouble(value)<=Double.parseDouble(avgHistoryValue)*(1+kpiDefine.getDeviationUp()/100);
				dataAbnormalStateBean.setNormal(isNormal);
				String desc="准确性异常 当前时间点指标值 "+value+",上门限 "+kpiDefine.getThreadHoldUp()+",下门限 "+kpiDefine.getThreadHoldDown()+"%";
				dataAbnormalStateBean.setDesc(desc);
				dataAbnormalStateBean.setHistoryValues(values);
				if(isNormal)
					dataAbnormalDao.removeDataAbnormal(dataSetId, kpiId, dimension, dataTime);
			}
			
			return dataAbnormalStateBean;
		}
		catch(Exception e){
			log.error("DataSetService getDataAbnormalStateBeanByParam error", e);
			e.printStackTrace();
		}
		return null;
	}
	
	public String selectDataValueByParamsFromSource(String dataSetId, String kpiId, String dimension, Date dataTime) {
		try {
			return dataSetDao.selectDataValueByParamsFromSource(dataSetId, kpiId, dimension, dataTime);
		} 
		catch (Exception e) {
			log.error("DataSetService selectDataValueByParamsFromSource error", e);
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 周报表查询
	 * */
	public List<DataSetStatiBean> selectDataSetStatiBeanFromStatiTable(Date startDate, Date endDate) {
		try {
			return dataSetDao.selectDataSetStatiBean(startDate, endDate);
		} 
		catch (Exception e) {
			log.error("DataSetService selectDataSetStatiBeanFromStatiTable error", e);
			e.printStackTrace();
		}
		return null;
	}
	
	public List<DataSetStatiBean> selectDataSetStatiBean(Date startDate, Date endDate,int dayCount) {
		try {
			return dataSetDao.selectDataSetStatiBean(startDate, endDate,dayCount);
		} 
		catch (Exception e) {
			log.error("DataSetService selectDataSetStatiBean error", e);
			e.printStackTrace();
		}
		return null;
	}
	
	public List<DataSetStatiBean> selectDataSetStatiBeanWithDayGranu(String datasetId, Date startDate, Date endDate) {
		try {
			return dataSetDao.selectDataSetStatiBeanWithDayGranu(datasetId,startDate, endDate);
		} 
		catch (Exception e) {
			log.error("DataSetService selectDataSetStatiBeanWithDayGranu error", e);
			e.printStackTrace();
		}
		return null;
	}
	
	public Map<Object, Map> selectDimensionNumByParamsFromSource(String dataSetId, Date start, Date end) {
		try {
			return dataSetDao.selectDimensionNumByParamsFromSource(dataSetId, start, end);
		} 
		catch (Exception e) {
			log.error("DataSetService selectDimensionNumByParamsFromSource error", e);
			e.printStackTrace();
		}
		return null;
	}
	
	public int selectDimensionNumByParamsFromSource(String dataSetId, Date dataTime) {
		try {
			return dataSetDao.selectDimensionNumByParamsFromSource(dataSetId, dataTime);
		} 
		catch (Exception e) {
			log.error("DataSetService selectDimensionNumByParamsFromSource error", e);
			e.printStackTrace();
		}
		return 0;
	}

	@Deprecated
	public String findDataSetIntegerDescByParam(String dataSetId, Date timeStamp) {
		if(dataSetId==null||timeStamp==null)
			return null;
		
		try {
//			return dataSetDao.findDataSetIntegerDescByParam(dataSetId, timeStamp);
		} 
		catch (Exception e) {
			log.error("DataSetService findDataSetIntegerDescByParam error", e);
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Atom_DatasetDefine> selectDatasetDefineByNeTypeAndCName(String name, String neType, boolean initialKpis) {
		try {
			return dataSetDao.selectDatasetDefineByNeTypeAndCName(name, neType, initialKpis);
		} 
		catch (Exception e) {
			log.error("DataSetService selectDatasetDefineByNeTypeAndCName error", e);
			e.printStackTrace();
		}
		return null;
	}


	public boolean saveDataSet(Map condition) {
		Date startCollectTime=null;
		Date firstCollectTime=null;
		Date endCollectTime=null;
		String backCollectNum=null;
		Double baseIntegerValue=null;
		Double deviation = null;
		endCollectTime = new Date();
		Atom_DatasetDefine  datasetDefine = new Atom_DatasetDefine();
		try {
			startCollectTime=new Date(Long.parseLong(condition.get("startCollectTime").toString()));
			firstCollectTime=new Date(Long.parseLong(condition.get("firstCollectTime").toString()));
			endCollectTime=new Date(Long.parseLong(condition.get("endCollectTime").toString()));
//			backCollectTime=new Date(Long.parseLong(condition.get("backCollectTime").toString()));
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		
		backCollectNum=condition.get("backCollectTime").toString().trim();
		String cn_name = condition.get("cn_name").toString().trim();
		String neType = condition.get("neType").toString().trim();
		String firstCollect=condition.get("firstCollect").toString().equals("null")?"1":"0";
		if(condition.get("baseIntegerValue").equals("")||condition.get("baseIntegerValue")==null){
		}else {
			 baseIntegerValue =Double.parseDouble(condition.get("baseIntegerValue").toString());
			
		}
		if(condition.get("deviation").equals("")||condition.get("deviation")==null){
			
		}else {
			deviation =Double.parseDouble(condition.get("deviation").toString());
			
		}
		
		String tableName = condition.get("tableName").toString().trim();
		String granularity = condition.get("granularity").toString().trim();
		String attentionType=condition.get("attentionType").toString();
		String timeUnit = condition.get("timeUnit").toString();
		String dbType=condition.get("dbType").toString();
		String host=condition.get("host").toString().trim();
		String port=condition.get("port").toString().trim();
		String sid=condition.get("sid").toString().trim();
		String userName=condition.get("userName").toString().trim();
		String passWord=condition.get("passWord").toString().trim();
		String timeStampColumn = condition.get("timeStampColumn").toString().trim();
		String publicNeColumn = condition.get("publicNeColumn").toString().trim();
		
		List<Atom_KpiDefine>  kpiDefines =null;
		
		datasetDefine.setCn_name(cn_name);
		datasetDefine.setNeType(neType);
		datasetDefine.setDeviation(deviation);
		datasetDefine.setBaseIntegerValue(baseIntegerValue);
		datasetDefine.setAttentionType(attentionType);
		//表配置
		datasetDefine.setTableName(tableName);
		datasetDefine.setGranularity(granularity);
		datasetDefine.setTimeUnit(timeUnit);
		datasetDefine.setStartCollectTime(startCollectTime);
		datasetDefine.setFirstStartTime(firstCollectTime);
		datasetDefine.setFirstEndTime(endCollectTime);
		datasetDefine.setTimeStampColumn(timeStampColumn);
		datasetDefine.setPublicNeColumn(publicNeColumn);
		datasetDefine.setBackCollectNum(backCollectNum);
		datasetDefine.setFirstCollect(firstCollect);
		//库配置
		datasetDefine.setDbType(dbType);
		datasetDefine.setHost(host);
		datasetDefine.setPort(port);
		datasetDefine.setSid(sid);
		datasetDefine.setUserName(userName);
		datasetDefine.setPassWord(passWord);
		datasetDefine.setKpiDefines(kpiDefines);
		//TODO
		try{
			dataSetDao.saveDatasetDefine(datasetDefine);
			
			/*
			try{
				CacheEditBean cacheEditBean=new CacheEditBean();
				cacheEditBean.setBeanClass(Atom_DatasetDefine.class);
				cacheEditBean.setBeanId(datasetDefine.getId());
				cacheEditBean.setOperationType("save");
				NoticeSender.getInstance().sendObject(BMSetting.dataCollectorHost, BMSetting.dataCollectorPort, cacheEditBean, false);
				log.info("DataSetService saveDataSet send dataSetId "+datasetDefine.getId()+" to dataCollector completed");
			}
			catch(Exception e){
				log.error("DataSetService saveDataSet send dataSetId "+datasetDefine.getId()+" to dataCollector error", e);
			}
			*/
		    return true;
		}
		catch(Exception e){
			log.error("DataSetService saveDataSet error", e);
			e.printStackTrace();
		}
		return false;
	}


	/**
	 * 界面的报表管理中删除报表
	 * */
	public boolean deleteDataSetById(String dataset_id) {
		boolean ret=false;
		try {
			ret=(Boolean)service.process("normal", "del", dataset_id);
		} catch (Exception e) {
			e.printStackTrace();
			ret=false;
		}
		return ret;
	}

	private boolean noticeAllByDelDataset(String dataset_id) {
		boolean ret=true;
		//1.采集端停止任务调度
		NoticeSender sender=NoticeSender.getInstance();
		sender.getLock().lock();
		try{
			DataSetCollectBean collectBean=new DataSetCollectBean();
			collectBean.setDataSetId(dataset_id);
			collectBean.setOperation(DataSetCollectBean.operation_unCollect);
			sender.sendObject(BMSetting.host, BMSetting.port, collectBean);
		}catch(Exception e)
		{
			log.info(dataset_id+" noticeAllByDelDataset in collectBean error");
			ret=false;
		}
		try{
			//2.校验端清理该报表缓存
			CacheEditBean cacheEditBean=new CacheEditBean();
			cacheEditBean.setBeanClass(Atom_DatasetDefine.class);
			cacheEditBean.setBeanId(dataset_id);
			cacheEditBean.setOperationType("remove");
			sender.sendObject(BMSetting.dataCollectorHost, BMSetting.dataCollectorPort, cacheEditBean, false);
			log.info("DataSetService removeDataSet send dataSetId "+dataset_id+" to dataCollector and dataMonitor completed");
			ret=true;
		}catch(Exception e)	
		{
			log.info(dataset_id+" noticeAllByDelDataset in CacheEditBean error");
			ret=false;
		}
		return ret;
	}


	public boolean delDatasetByDatasetIDs(List<String> datasetIDs) {
		try{
			dataSetDao.delDatasetByDatasetIDs(datasetIDs);
		    return true;
		}
		catch(Exception e){
			log.error("DataSetService deleteDataSetById error", e);
			e.printStackTrace();
		}
		return false;
	}

	public boolean editDataSet(Map condition) {
		Date startCollectTime=null;
		Date firstCollectTime=null;
		Date endCollectTime=null;
		String backCollectNum=null;
		Double baseIntegerValue=null;
		Double deviation = null;
		endCollectTime = new Date();
		Atom_DatasetDefine  datasetDefine = new Atom_DatasetDefine();
		try {
			startCollectTime=new Date(Long.parseLong(condition.get("startCollectTime").toString()));
			firstCollectTime=new Date(Long.parseLong(condition.get("firstCollectTime").toString()));
			endCollectTime=new Date(Long.parseLong(condition.get("endCollectTime").toString()));
//			backCollectTime=new Date(Long.parseLong(condition.get("backCollectTime").toString()));
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		backCollectNum=condition.get("backCollectTime").toString();
		String cn_name = condition.get("cn_name").toString();
		String neType = condition.get("neType").toString();
		String firstCollect=condition.get("firstCollect").toString().equals("null")?"1":"0";
		if(condition.get("baseIntegerValue").equals("")||condition.get("baseIntegerValue")==null){
		}else {
			 baseIntegerValue =Double.parseDouble(condition.get("baseIntegerValue").toString());
			
		}
		if(condition.get("deviation").equals("")||condition.get("deviation")==null){
			
		}else {
			deviation =Double.parseDouble(condition.get("deviation").toString());
			
		}
		String id = condition.get("id").toString();
		String tableName = condition.get("tableName").toString();
		String granularity = condition.get("granularity").toString();
		String attentionType=condition.get("attentionType").toString();
		String timeUnit = condition.get("timeUnit").toString();
		String dbType=condition.get("dbType").toString();
		String host=condition.get("host").toString();
		String port=condition.get("port").toString();
		String sid=condition.get("sid").toString();
		String userName=condition.get("userName").toString();
		String passWord=condition.get("passWord").toString();
		String timeStampColumn = condition.get("timeStampColumn").toString();
		String publicNeColumn = condition.get("publicNeColumn").toString();
		
		List<Atom_KpiDefine>  kpiDefines =null;
		datasetDefine.setId(id);
		datasetDefine.setCn_name(cn_name);
		datasetDefine.setNeType(neType);
		datasetDefine.setDeviation(deviation);
		datasetDefine.setBaseIntegerValue(baseIntegerValue);
		datasetDefine.setAttentionType(attentionType);
		//表配置
		datasetDefine.setTableName(tableName);
		datasetDefine.setGranularity(granularity);
		datasetDefine.setTimeUnit(timeUnit);
		datasetDefine.setStartCollectTime(startCollectTime);
		datasetDefine.setFirstStartTime(firstCollectTime);
		datasetDefine.setFirstEndTime(endCollectTime);
		datasetDefine.setTimeStampColumn(timeStampColumn);
		datasetDefine.setPublicNeColumn(publicNeColumn);
		datasetDefine.setBackCollectNum(backCollectNum);
		datasetDefine.setFirstCollect(firstCollect);
		//库配置
		datasetDefine.setDbType(dbType);
		datasetDefine.setHost(host);
		datasetDefine.setPort(port);
		datasetDefine.setSid(sid);
		datasetDefine.setUserName(userName);
		datasetDefine.setPassWord(passWord);
		datasetDefine.setKpiDefines(kpiDefines);
		//TODO
		try{
			dataSetDao.updateDatasetDefine(datasetDefine);
			
			/*
			try{
				CacheEditBean cacheEditBean=new CacheEditBean();
				cacheEditBean.setBeanClass(Atom_DatasetDefine.class);
				cacheEditBean.setBeanId(id);
				cacheEditBean.setOperationType("edit");
				NoticeSender.getInstance().sendObject(BMSetting.dataCollectorHost, BMSetting.dataCollectorPort, cacheEditBean, false);
				log.info("DataSetService editDataSet send dataSetId "+id+" to dataCollector completed");
			}
			catch(Exception e){
				log.error("DataSetService editDataSet send dataSetId "+id+" to dataCollector error", e);
			}
			*/
		    return true;
		}
		catch(Exception e){
			log.error("DataSetService editDataSet error", e);
			e.printStackTrace();
			return false;
		}
	}
	
	public List<Atom_DatasetDefine> getAllDatasetDefine() {
		try{
			return dataSetDao.getAllDataSetDefine();
		}
		catch(Exception e){
			log.error("IDataSetService getAllAtom_KpiDefine error", e);
		}
		
		return null;
	}

	public List<TreeNode> getAllDatasetNode(String id) {
		try{
			return null;//dataSetDao.getAllDatasetNode(id);
		}
		catch(Exception e){
			log.error("IDataSetService getAllDatasetNode error", e);
		}

		return null;
	}
	
	
	public ReturnNode addDatasetByArgs(DatasetBean args){
		
		for(KpiAndThBean b:args.getKpiAndThs()){
			
			System.out.println(b);
		}
		ReturnNode ret=new ReturnNode();
		ret.setOperatorArg(args);
		ret.setSuccess(false);
		
		try{
			checkDatasetBean(args);
		}catch (Exception e) {
			ret.setReturnStr(e.getMessage());
			return ret;
		}
		//if(dataSetDao.isDatasetExist(args.getDataset_id())){
			//ret.setReturnStr("dataset ID is exist !");
			//return ret;
		//}
		
		Atom_DatasetDefine datasetDefine=DatasetDefineUtil.createDatasetDefine(args);
		List<Atom_DatasetNe> datasetNes=DatasetDefineUtil.createDatasetNes(args);
		List<Atom_KpiDefine> kpiDefines=KpiAndThreadholdUtil.createKpiDefines(args);
		List<Atom_NeSpecialThreadhold> neSpecialThreadholds=KpiAndThreadholdUtil.createNeSpecialThreadhold(args);
		
		//dataSetDao.saveDatasetDefine(datasetDefine);
		//dataSetNeDao.saveDatasetNes(datasetNes);
		//kpiDefineDao.saveKpiDefines(kpiDefines);
		//neSpecialThreadHoldDao.saveNeSpecialThreadHolds(neSpecialThreadholds);
		ret.setSuccess(true);
		
		return ret;
	}
	
	public ReturnNode editDatasetByArgs(DatasetBean bean){
		
		ReturnNode node1=deleteDatasetByDatasetID(bean.getDataset_id());
		if(!node1.isSuccess())
			return node1;
		ReturnNode node2=addDatasetByArgs(bean);
		if(!node2.isSuccess())
			return node2;
		
		return node2;
	}
	
	public ReturnNode deleteDatasetByDatasetID(String datasetID){
		
		ReturnNode ret=new ReturnNode();
		ret.setOperatorArg(datasetID);
		ret.setSuccess(false);
		try{
		if (datasetID == null || datasetID.trim().equals("")) {
				ret.setReturnStr("Dataset Id is null");
				return ret;
			}
			dataSetDao.delDatasetByID(datasetID);
			//dataSetNeDao.delDatasetNeByDatasetID(datasetID);
			kpiDefineDao.delKpiDefineByDatasetID(datasetID);
			//neSpecialThreadHoldDao.delNeSpecialThreadHoldByDatasetID(datasetID);
			ret.setSuccess(true);
			return ret;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("DataSetService deleteDatasetByDatasetID is error!",e);
		}
		return ret;
	}
	
	public List<MapEntryNode> getAllDatasetMapNode(){
		List<MapEntryNode> retVal=new ArrayList<MapEntryNode>();
		//DatasetDefineUtil.c
		try {
			for (Atom_DatasetDefine def : dataSetDao.getAllDataSetDefine()) {
				retVal.add(DatasetDefineUtil.createMapEntryBeanByDataset(def));
			}
			return retVal;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("dataSetDao.getAllDataSetDefine() is error!",e);
		}
		return retVal;
	}

	
	private void checkDatasetBean(DatasetBean bean) throws Exception{
		if(bean==null)
			throw new Exception("the DatasetBean is null");
		if(bean.getDataset_id()==null)
			throw new Exception("the Dataset Id is null");
		
	}
	
	public static void main(String[] args){
		double a =java.lang.Math.round(0.22222*100)/100.00;
		System.out.println(a);
	}


	/*public boolean saveDataSet(Atom_DatasetDefine datasetDefine) {
		// TODO Auto-generated method stub
		return false;
	}*/


	public List<DataSetStateBean> selectDataSetStateByTimeStamp(String dataSetId, long startTime, long endTime){
		try{
			return dataSetDao.selectDataSetStateByTimeStamp(dataSetId, startTime, endTime);
		}
		catch(Exception e){
			log.error("DataSetService selectDataSetStateByTimeStamp() is error!",e);
		}
		return null;
	}
   
	
	public List<NeTypeDataSetBean> getHissgsn(String dataSetId, long startTime, long endTime){
		List<NeTypeDataSetBean> list = new ArrayList<NeTypeDataSetBean>();
		try{
			List<DataSetStateBean> sgsn =  dataSetDao.selectDataSetStateByTimeStamp(dataSetId, startTime, endTime);
			NeTypeDataSetBean bean= new NeTypeDataSetBean();
			bean.setTitle("历史数据");
			bean.setSgsn(sgsn);
			list.add(bean);
			return list;
		}
		catch(Exception e){
			log.error("DataSetService selectDataSetStateByTimeStamp() is error!",e);
		}
		return null;
	}

	public List<NeTypeDataSetBean> selectDataSetStateWithLastTimeStampDistinguishByNeType(String attentionType){
		try{
			return dataSetDao.selectDataSetStateWithLastTimeStampDistinguishByNeType(attentionType);
		}
		catch(Exception e){
			log.error("DataSetService selectDataSetStateWithLastTimeStampDistinguishByNeType() is error!",e);
		}
		return null;
	}


	public List<KpiStateBean> selectKpiStateByDataSetIdAndTimeStamp(String dataSetId, Date timeStamp) {
		try{
			return dataSetDao.selectKpiStateByDataSetIdAndTimeStamp(dataSetId, timeStamp);
		}
		catch(Exception e){
			log.error("DataSetService selectKpiStateByDataSetIdAndTimeStamp() is error!",e);
		}
		return null;
	}


	public List<NeTypeDataSetStatiBean> selectAllDataSetStatiBean() {
		try{
			return dataSetDao.selectAllDataSetStatiBean();
		}
		catch(Exception e){
			log.error("DataSetService selectAllDataSetStatiBean() is error!",e);
		}
		return null;
	}


	public List<NeTypeDataSetStatiBean> selectDataSetStatiBeanByNeType(String neType, int span,String spanType) {
		try{
			return dataSetDao.selectDataSetStatiBeanByNeType(neType,span, spanType);
		}
		catch(Exception e){
			log.error("DataSetService selectDataSetStatiBeanByNeType() is error!",e);
		}
		return null;
	}
	
	//获取最近一次的柱状图DateXML
    public String getNearDateXMl(){
    	List<NeTypeDataSetStatiBean> netList = selectAllDataSetStatiBean();
    	StringBuilder sb = new StringBuilder("<data>");
    	StringBuilder x1=new StringBuilder("<series name='完整数'>");
		StringBuilder x2=new StringBuilder("<series name='准确数'>");
		StringBuilder x3=new StringBuilder("<series name='总数'>");
    	for(NeTypeDataSetStatiBean ns:netList){
    		x1.append("<point name='"+ns.getTypeName().toUpperCase()+"("+df.format(ns.getTimeStamp())+")' y='"+ns.getUnIntegerSum()+"'/>");
    		x2.append("<point name='"+ns.getTypeName().toUpperCase()+"("+df.format(ns.getTimeStamp())+")' y='"+ns.getUnNormalSum()+"'/>");
    		x3.append("<point name='"+ns.getTypeName().toUpperCase()+"("+df.format(ns.getTimeStamp())+")' y='"+ns.getDataSetSum()+"'/>");
    	}
    	x1.append("</series>");
    	x2.append("</series>");
    	x3.append("</series>");
    	sb.append(x1);
    	sb.append(x2);
    	sb.append(x3);
    	sb.append("</data>");
    	return sb.toString();
    }
    
  //获取最近一次的柱状图比率DateXML
    public String getNearDateXMl2(){
    	List<NeTypeDataSetStatiBean> netList = selectAllDataSetStatiBean();
    	StringBuilder sb = new StringBuilder("<data>");
    	StringBuilder x1=new StringBuilder("<series name='完整率'>");
		StringBuilder x2=new StringBuilder("<series name='准确率'>");
    	for(NeTypeDataSetStatiBean ns:netList){
    		x1.append("<point name='"+ns.getTypeName().toUpperCase()+"("+df.format(ns.getTimeStamp())+")' y='"+java.lang.Math.round(((double)ns.getUnIntegerSum()/(double)ns.getDataSetSum())*100)+"'/>");
    		x2.append("<point name='"+ns.getTypeName().toUpperCase()+"("+df.format(ns.getTimeStamp())+")' y='"+java.lang.Math.round(((double)ns.getUnNormalSum()/(double)ns.getDataSetSum())*100)+"'/>");
    	}
    	x1.append("</series>");
    	x2.append("</series>");
    	sb.append(x1);
    	sb.append(x2);
    	sb.append("</data>");
    	return sb.toString();
    }
  //获取历史拆线DateXML
    public String getHisDateXMl2(String neType,int day , String spanType){
    	List<NeTypeDataSetStatiBean> netList = selectDataSetStatiBeanByNeType(neType,day,spanType);
    	if(netList==null){return "false";}
    	StringBuilder sb = new StringBuilder("<data>");
    	StringBuilder x1=new StringBuilder("<series name='完整性'>");
		StringBuilder x2=new StringBuilder("<series name='准确性'>");
    	for(NeTypeDataSetStatiBean ns:netList){
    		x1.append("<point name='"+df.format(ns.getTimeStamp())+"' y='"+ns.getUnIntegerRatio()+"'/>");
    		x2.append("<point name='"+df.format(ns.getTimeStamp())+"' y='"+ns.getUnNormalRatio()+"'/>");
    		
    	}
    	x1.append("</series>");
    	x2.append("</series>");
    	sb.append(x1);
    	sb.append(x2);
    	sb.append(x2);
    	sb.append("</data>");
    	return sb.toString();
    }
    
    //获取柱状图
    public String getHisDateXMl(String neType,int day , String spanType){
    	List<NeTypeDataSetStatiBean> netList = selectDataSetStatiBeanByNeType(neType,day,spanType);
    	if(netList==null){return "false";}
    	StringBuilder sb = new StringBuilder("<data>");
    	StringBuilder x1=new StringBuilder("<series name='完整数'>");
		StringBuilder x2=new StringBuilder("<series name='准确数'>");
		StringBuilder x3=new StringBuilder("<series name='总数'>");
    	for(NeTypeDataSetStatiBean ns:netList){
    		x1.append("<point name='"+df.format(ns.getTimeStamp())+"' y='"+ns.getUnIntegerSum()+"'/>");
    		x2.append("<point name='"+df.format(ns.getTimeStamp())+"' y='"+ns.getUnNormalSum()+"'/>");
    		x3.append("<point name='"+df.format(ns.getTimeStamp())+"' y='"+ns.getDataSetSum()+"'/>");
    		
    	}
    	x1.append("</series>");
    	x2.append("</series>");
    	x3.append("</series>");
    	sb.append(x1);
    	sb.append(x2);
    	sb.append(x3);
    	sb.append("</data>");
    	return sb.toString();
    }
    
    public List<NeTypeDataSetBean> TestDate(String attentionType){
    	List<NeTypeDataSetBean> result=new ArrayList<NeTypeDataSetBean>();
    	for(int i=0;i<3;i++){
    	List<DataSetStateBean> sgsn=new ArrayList<DataSetStateBean>();
    	NeTypeDataSetBean bean = new NeTypeDataSetBean();
    	bean.setTitle("title1");
    	bean.setNeTypeId("title"+i);
    	for(int j=0;j<5;j++){
    		DataSetStateBean dBean= new DataSetStateBean();
    		dBean.setDataSetId("baobiao"+i+""+j);
    		dBean.setDatsSetName("baobiao"+i+""+j);
    		dBean.setInteger((i+j)/2==0?false:true);
    		dBean.setIntegerRatio(20.0);
    		dBean.setNeTypeId("baobiao"+i+""+j);
    		dBean.setNeTypeName("baobiao"+i+""+j+"NeTyp");
    		dBean.setTimeStamp(new Date());
    		dBean.setValueCorrect(i*j/2==0?false:true);
    		 sgsn.add(dBean);
    	}
    	bean.setSgsn(sgsn);
    	result.add(bean);
    	}
		return result;
	}
    
    /**
     * 天报表查询
     * */
    public List<DataSetStatiBean> selectAllDataSetStatiBeanWithDayGranu(Date startDate,Date endDate)
	{
		List<DataSetStatiBean> ret=new ArrayList<DataSetStatiBean>();
//		try {
//			List<String> dataset_ids=dataSetDao.selectAllDatasetId();
//			for(String dataset_id:dataset_ids)
//			{
//				List<DataSetStatiBean> temp=this.selectDataSetStatiBeanWithDayGranu(dataset_id, startDate, endDate);
//				ret.addAll(temp);
//			}
//		} catch (Exception e) {
//			log.error("DataSetService selectAllDataSetStatiBeanWithDayGranu error", e);
//			e.printStackTrace();
//		}
		
		try {
			ret=dataSetDao.selectAllDataSetStatiBeanWithDayGranuByTable(startDate, endDate);
		} catch (Exception e) {
			log.error("DataSetService selectAllDataSetStatiBeanWithDayGranu error", e);
			e.printStackTrace();
		}
		return ret;
	}

    @Deprecated
	public void noticeBackCollectByTimeStamp(String datasetId, Date startDate) throws Exception {
		Atom_DatasetDefine datasetDefine=dataSetDao.findById(datasetId, true);
		DataSetCollectBean datasetCollectBean=this.wrapDataSetCollectBeanByDataSet(datasetDefine);
		Date endDate=new Date(startDate.getTime()+Long.valueOf(datasetCollectBean.getTimeUnit())*1000);
		datasetCollectBean.setFirstStartTime(startDate);
		datasetCollectBean.setFirstEndTime(endDate);
//		datasetCollectBean.setOperation(DataSetCollectBean.operation_backCollect);
		datasetCollectBean.setStartCollectTime(new Date());
		
		NoticeSender.getInstance().sendObject("127.0.0.1", 5000, datasetCollectBean);
	}
	
    @Deprecated
	public void noticeBackCollectByTimeStamp(String datasetId, List<Date> startDates) throws Exception {
		for(Date date:startDates)
		{
			this.noticeBackCollectByTimeStamp(datasetId, date);
			Thread.currentThread().sleep(1000);
		}
	}

	public String collectData(String datasetId, List<String> sDates) {
		String ret="";
		try {
			if(sDates.isEmpty())
				return "本次补录失败,没有需要补录的时间点";
			List<Date> dates=new ArrayList<Date>();
			for(String sdate:sDates)
			{
				dates.add(DateUtil.StringToDate(sdate));
			}
			//1.补录数据
			int collectCount=dataSetDao.collectData(datasetId, dates);
			//2.更新改报表的天统计表
			Collections.sort(dates);
			Date start=CharliDate.getStartTimeOfDaysApartCurrentDay(dates.get(0), 0);
			Date end=CharliDate.getStartTimeOfDaysApartCurrentDay(dates.get(dates.size()-1), 1);
			List<DataSetStatiBean> statibeans=dataSetDao.selectDataSetStatiBeanWithDayGranu(datasetId, start, end);
			dataSetDao.removeDataSetStatisByParam(datasetId, start, end);
			dataSetDao.saveDataSetStatisByJdbc(statibeans);
			ret="补录完成，本次补录数据条数："+collectCount;
		} catch (Exception e) {
			e.printStackTrace();
			ret="补录失败，失败原因："+e.getMessage();
		}
		return ret;
	}
	
	public boolean collectData(Map map) {
		String dataset_id = map.get("dataid").toString();
		Date startDate = new Date(Long.valueOf((String) map.get("start")));
		Date endDate = new Date(Long.valueOf((String) map.get("end")));
		boolean ret = true;
		try {
			service.process(CollectType.ONCE_COLLECT,
					CollectType.ADD_CONDITION, dataset_id, startDate, endDate);
		} catch (Exception e) {
			e.printStackTrace();
			ret = false;
		}
		return ret;
	}

	public IntegerDescBean selectIntegerDesc(String dataset_id,Date date)
	{
		try {
			Map<String,Object> descInfo=dataSetDao.findDataSetIntegerDescByParam(dataset_id, date);
//			List<String> dimensions=dataSetDao.selectDimensionsFromSource(dataset_id, date);	//可设计为，当在完整性详情点击”实时源表维度查询“予以调用
			List<String> dimensions=dataSetDao.currentDimensionList(dataset_id, date);
			IntegerDescBean	ret=new IntegerDescBean();
			ret.setDesc((String)descInfo.get("abnormal_desc"));
			ret.setDate((Date)descInfo.get("data_time"));
			ret.setDimensions(dimensions);
			return ret;
		} catch (Exception e) {
			log.error("DataSetService.selectIntegerDesc exception",e);
		}
		return null;
	}
	
	public List<CollectDescBean> selectCollectDesc2(String dataset_id,Date date)
	{
		List<CollectDescBean> ret=new ArrayList<CollectDescBean>();
		try {
			//1.查到报表对应的所有采集任务
			List<ReportMapping> reportMappings=mappingDao.queryByDataSetId(dataset_id);
			//2.遍历每个采集任务在指定时间的采集情况
			for(ReportMapping mapping:reportMappings)
			{
				CollectDescBean descBean=null;
				List<CollectMonitor> collectMonitors=new ArrayList<CollectMonitor>();
				String hostIp=mapping.getHostIp();
				String taskId=mapping.getTaskid();
				collectMonitors=monitorDao.queryByConditions(hostIp, taskId, date);
				descBean=CollectMonitorUtil.createCollectDescBean(collectMonitors, mapping);
				ret.add(descBean);
			}
		} catch (Exception e) {
			log.error("DataSetService.selectCollectDesc2 exception",e);
		}
		return ret;
	}
	//以后统一，只在Dao层的查询方法命名上加入参数信息
	//Atom对象到client层对象的转换交由专门的类执行，不在Dao层转换
	public CollectDescBean selectCollectDesc(String dataset_id,String dimension,Date date)
	{
		CollectDescBean ret=null;
		try {
			ReportMapping reportMapping=mappingDao.queryByDataSetIdAndDimension(dataset_id, dimension);
			String hostip=reportMapping.getHostIp();
			String taskid=reportMapping.getTaskid();
			List<CollectMonitor> collectMonitors=monitorDao.queryByConditions(hostip, taskid, date);
			ret=CollectMonitorUtil.createCollectDescBean(collectMonitors, reportMapping);
			
		} catch (Exception e) {
			log.error("DataSetService.selectCollectDesc exception", e);
		}
		return ret;
	}
}
